package com.work.game.home.ui.activity.agent;


import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.android.volley.Request;
import com.work.game.R;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.adapter.IncomeListAdapter;
import com.work.game.home.ui.model.statistics.RankingList;
import com.work.game.home.ui.model.vo.BaseVo;
import com.work.game.volley.BaseActivity;
import com.work.game.volley.httpUtil.HUCDataAnalysis;
import com.work.game.volley.httpUtil.interfase.HUCDataAnalysisInterfase;
import com.work.game.volley.state.HUCState;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class DataCenterActivity extends BaseActivity {

    private HUCDataAnalysisInterfase hucDataAnalysisInterfase;

    private TextView apprenticeCount, apprenticeCountInsert, apprenticeCountActive, income, incomeMGC, incomeMRP, incomeMonth, incomeMonthMGC, incomeMonthMRP, incomeToDay, incomeToDayMGC, incomeToDayMRP;

    private ListView tvIncomeList, tvMonthList, tvTodayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_data_center);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("数据中心");
        hucDataAnalysisInterfase = HUCDataAnalysis.getInstance();
        initData();
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.ll_count).setOnClickListener(this);
        findViewById(R.id.ll_month).setOnClickListener(this);
        findViewById(R.id.ll_day).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_count:
                sendIntent(this, TheChartsActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_1));
                break;
            case R.id.ll_month:
                sendIntent(this, TheChartsActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_2));
                break;
            case R.id.ll_day:
                sendIntent(this, TheChartsActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_3));
                break;
            default:
                break;
        }
    }


    @Override
    public void initView() {
        super.initView();
        income = findViewById(R.id.income);
        incomeMGC = findViewById(R.id.income_mgc);
        incomeMRP = findViewById(R.id.income_mrp);
        incomeMonth = findViewById(R.id.income_month);
        incomeMonthMGC = findViewById(R.id.income_month_mgc);
        incomeMonthMRP = findViewById(R.id.income_month_mrp);
        incomeToDay = findViewById(R.id.income_today);
        incomeToDayMGC = findViewById(R.id.income_today_mgc);
        incomeToDayMRP = findViewById(R.id.income_today_mrp);
        apprenticeCount = findViewById(R.id.apprentice_count);
        apprenticeCountInsert = findViewById(R.id.apprentice_count_insert);
        apprenticeCountActive = findViewById(R.id.apprentice_count_active);

        tvIncomeList = findViewById(R.id.lv_income_list);
        tvMonthList = findViewById(R.id.lv_month_list);
        tvTodayList = findViewById(R.id.lv_today_list);
    }

    private void initData() {
        loading();
        BaseVo baseVo = new BaseVo();
        baseVo.setToken(getSharedPreferences("deviceId"));
        hucRequestInterfase.executeStr(this, null, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_USER_DATA_CENTER, hucParameInterfase.getRequestParameter(baseVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                loadDismiss();
                try {
                    JSONObject json = new JSONObject((String) response);
                    apprenticeCount.setText(json.getString("apprenticeCount"));
                    apprenticeCountInsert.setText(json.getString("apprenticeCountInsert"));
                    apprenticeCountActive.setText(json.getString("apprenticeCountActive"));
                    income.setText(json.getString("income"));
                    incomeMGC.setText(json.getString("incomeMGC"));
                    incomeMRP.setText(json.getString("incomeMRP"));
                    incomeMonth.setText(json.getString("incomeMonth"));
                    incomeMonthMGC.setText(json.getString("incomeMonthMGC"));
                    incomeMonthMRP.setText(json.getString("incomeMonthMRP"));
                    incomeToDay.setText(json.getString("incomeToDay"));
                    incomeToDayMGC.setText(json.getString("incomeToDayMGC"));
                    incomeToDayMRP.setText(json.getString("incomeToDayMRP"));
                    //排行榜展示
                    String incomeListStr = json.getString("incomeList");
                    String monthListStr = json.getString("monthList");
                    String todayListStr = json.getString("todayList");

                    List<RankingList> incomeList = (List<RankingList>) hucDataAnalysisInterfase.parseJsonList(RankingList.class, incomeListStr);
                    IncomeListAdapter incomeListAdapter = new IncomeListAdapter(this, incomeList);
                    tvIncomeList.setAdapter(incomeListAdapter);
                    //setListViewHeightBasedOnChildren(tvIncomeList);
                    List<RankingList> monthList = (List<RankingList>) hucDataAnalysisInterfase.parseJsonList(RankingList.class, monthListStr);
                    IncomeListAdapter monthListAdapter = new IncomeListAdapter(this, monthList);
                    tvMonthList.setAdapter(monthListAdapter);
                    //setListViewHeightBasedOnChildren(tvMonthList);
                    List<RankingList> todayList = (List<RankingList>) hucDataAnalysisInterfase.parseJsonList(RankingList.class, todayListStr);
                    IncomeListAdapter todayListAdapter = new IncomeListAdapter(this, todayList);
                    tvTodayList.setAdapter(todayListAdapter);
                    //setListViewHeightBasedOnChildren(tvTodayList);
                } catch (JSONException e) {
                    e.printStackTrace();
                    showToast(e.getMessage());
                }
                break;
        }
    }

    /**
     * 动态设置Listview 高度
     *
     * @param listView
     */
    private void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        IncomeListAdapter listAdapter = (IncomeListAdapter) listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, null);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight;
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}